//
//  XJTieZiDetailCell.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/7.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  帖子详情cell -- cell 又嵌套一个tableView



#import "XJTieZiDetailCell.h"

#import "XJChildCommentView.h" // 子评论View;作为cell的一部分,内部放的是一个tableView。
#import "XJTieZiDetailModel.h" //  XJTieZiDetailCommentModel
@interface XJTieZiDetailCell()
@property(nonatomic, strong) UIImageView *iconIv;

@property(nonatomic, strong) UILabel *nameLb;
@property(nonatomic, strong) UILabel *timeLb; //
@property(nonatomic, strong) UIButton *commentButton; //评论按钮(图标+删除文字 更具customer_id判断是自己(显示“删除”文字) 还是别人(显示“评论”按钮))

@property(nonatomic, strong) UILabel *tieZiContentLb;//评论内容。高度自适应。

// 预留:自评论view。 又是一个tableView
@property(nonatomic, strong) XJChildCommentView *childCommentView;


/**6-8 用于记录button的状态: 没有登录 、 删除 、 评论*/
@property(nonatomic, assign) BtnType btnType;


@end

@implementation XJTieZiDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

#pragma mark - 一 初始化

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupView];
        
        [self useSDAutoLayoutToSetupLayout];
    }
    return self;
}

- (void)setupView {
    [self.contentView addSubview:self.iconIv];
    [self.contentView addSubview:self.nameLb];
    [self.contentView addSubview:self.timeLb];
    [self.contentView addSubview:self.commentButton];
    [self.contentView addSubview:self.tieZiContentLb];
    [self.contentView addSubview:self.childCommentView];
}

#pragma mark - 三 布局
- (void)useSDAutoLayoutToSetupLayout {
    
    CGFloat kLeftMargin = 15.0;
    CGFloat k5 = 5;
    
    self.iconIv.sd_layout.leftSpaceToView(self.contentView, kLeftMargin)
    .topSpaceToView(self.contentView, k5)
    .widthIs(40)
    .heightIs(40);
    
    self.nameLb.sd_layout.leftSpaceToView(self.iconIv, k5)
    .topSpaceToView(self.contentView, k5)
    .heightIs(20)
    .widthIs(50);
    
    self.timeLb.sd_layout.leftSpaceToView(self.nameLb, k5)
    .topSpaceToView(self.contentView, k5)
    .heightIs(20)
    .widthIs(100);
    
    self.commentButton.sd_layout.topSpaceToView(self.contentView, k5)
    .rightSpaceToView(self.contentView, kLeftMargin)
    .heightIs(20)
    .widthIs(50);
    
    // 和label的左边对齐
    self.tieZiContentLb.sd_layout.topSpaceToView(self.iconIv, k5)
    .leftEqualToView(self.nameLb)
    .widthIs(KWidth - 40 - k5*2 - kLeftMargin);
    
    self.childCommentView.sd_layout.topSpaceToView(self.tieZiContentLb, k5)
    .leftEqualToView(self.tieZiContentLb)
    .widthIs(KWidth - 40 - k5*2 - kLeftMargin);
}

#pragma mark - 二 lazy
//2.
- (UIImageView *)iconIv {
    if (!_iconIv) {
        _iconIv = [UIImageView new];
        _iconIv.layer.masksToBounds = YES;
        _iconIv.layer.cornerRadius = 20;
    }
    return _iconIv;
}


// 3-1
- (UILabel *)nameLb {
    if (!_nameLb) {
        _nameLb = [UILabel new];
//        _nameLb.backgroundColor = [UIColor brownColor];
        _nameLb.font = XJFONTSET_R(14.0);
    }
    return _nameLb;
}
// 3-2
- (UILabel *)timeLb {
    if (!_timeLb) {
        _timeLb = [UILabel new];
//        _timeLb.backgroundColor = [UIColor redColor];
        _timeLb.font = XJFONTSET_R(14.0);
    }
    return _timeLb;
}
// 3-3
- (UIButton *)commentButton {
    if (!_commentButton) {
        _commentButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        // 一开始不给图片 不给文字 根据服务器返回的数据:动态的控制是显示图片 还是 显示文字
//        [_commentButton setImage:[UIImage imageNamed:@"nav_message"] forState:(UIControlStateNormal)];
        [_commentButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
        _commentButton.titleLabel.font = XJFONTSET_R(14.0);
        
        [_commentButton addTarget:self action:@selector(clickCommentDeleteButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _commentButton;
}

- (UILabel *)tieZiContentLb {
    if (!_tieZiContentLb) {
        _tieZiContentLb = [[UILabel alloc] init];
//        _tieZiContentLb.backgroundColor = [UIColor lightGrayColor];
        _tieZiContentLb.numberOfLines = 0 ;// 自动换行
    }
    return _tieZiContentLb;
}

- (XJChildCommentView *)childCommentView {
    if (!_childCommentView) {
        _childCommentView = [[XJChildCommentView alloc] initWithFrame:CGRectMake(0, 0, KWidth - 65, 0)];
//        _childCommentView.backgroundColor = [UIColor purpleColor];
    }
    return _childCommentView;
}

#pragma mark - 四 setter赋值
- (void)setCommentModel:(XJTieZiDetailCommentModel *)commentModel {
    _commentModel = commentModel;
    
    UIImage *placehoderImg = [UIImage imageNamed:@"default_head"];
    
    [self.iconIv sd_setImageWithURL:[NSURL URLWithString:commentModel.avatar] placeholderImage:placehoderImg];
    
    self.nameLb.text = commentModel.nickname;
    self.timeLb.text = commentModel.publish_time;
    
    
    // 没登录:
    // 控制commentButton 的显示状态
    if ([self rb_customId]) {
       
        if ([commentModel.customer_id isEqualToString:[self rb_customId]]) {
//            XJLog(@"自己人 显示删除文字");
            self.btnType = BtnTypeDelete;
            [self.commentButton setTitle:@"删除" forState:(UIControlStateNormal)];
            [self.commentButton setImage:[UIImage imageNamed:@""] forState:(UIControlStateNormal)];
        }else {
//            XJLog(@"别人  显示评论按钮");
            self.btnType = BtnTypeComment;
            [self.commentButton setTitle:@"" forState:(UIControlStateNormal)];
            [self.commentButton setImage:[UIImage imageNamed:@"nav_message"] forState:(UIControlStateNormal)];
        }
    }else {
        XJLog(@"没登录 显示评论按钮");
        self.btnType = BtnTypeNoLogin;
        [self.commentButton setTitle:@"" forState:(UIControlStateNormal)];
        [self.commentButton setImage:[UIImage imageNamed:@"nav_message"] forState:(UIControlStateNormal)];

    }
    
    

    
    // 计算高度
    self.tieZiContentLb.text = commentModel.text;
    // 给高度赋值:
    CGFloat tieZiContentLB_h = [self.childCommentView heightForStr:commentModel.text];
    self.tieZiContentLb.sd_layout.heightIs(tieZiContentLB_h);
    
    
    // 给子评论数组赋值。
    // 计算childCommentView的高度并重新赋值
    CGFloat childCommentView_h = [self totalHeightOfTableViewWithArray:self.commentModel.child_comments];
    self.childCommentView.sd_layout.heightIs(childCommentView_h);
    
    self.childCommentView.childCommentArray = self.commentModel.child_comments;

    // 核心代码:✅
    [self setupAutoHeightWithBottomView:self.childCommentView bottomMargin:10];
}


#pragma mark - 五 事件回调。

- (void)clickCommentDeleteButtonAction:(UIButton *)btn {
    
    
//    XJLog(@"按钮的状态:%ld",(long)self.btnType);
//    switch (self.btnType) {
//        case BtnTypeNoLogin:
//            XJLog(@"BtnTypeNoLogin");
//            break;
//        case BtnTypeDelete:
//            XJLog(@"BtnTypeDelete");
//            break;
//        case BtnTypeComment:
//            XJLog(@"BtnTypeComment");
//            break;
//        default:
//            break;
//    }
    
    if (self.tieziDetailCellDeleteCommentBtnBlock) {
        self.tieziDetailCellDeleteCommentBtnBlock(self.btnType);
    }
}

#pragma mark - 六 private Method

// 1.根据model 返回 评论内容。
- (NSString *)commentStrWithModel:(XJTieZiDetailChildCommentModel *)model {
    NSString *commentStr = nil;
    
    if ([model.level isEqualToString:@"3"]) {
        commentStr = [NSString stringWithFormat:@"%@ 回复 %@:%@ %@",model.nickname,model.target_nickname,model.text,model.publish_time];
    }else {
        commentStr = [NSString stringWithFormat:@"%@:%@ %@",model.target_nickname,model.text,model.publish_time];
    }
    
    return commentStr;
}

// 2.根据文字长度计算 对应的label的高度
- (CGFloat)heightForStr:(NSString *)str {
    CGFloat maxWidth = KWidth - 65;
    
    UILabel *label = [UILabel new];
    label.text = str;
//    label.font = [UIFont systemFontOfSize:14.0];
    label.font = [UIFont systemFontOfSize:18.0];

    label.numberOfLines = 0;
    
    CGSize size = [label sizeThatFits:CGSizeMake(maxWidth, CGFLOAT_MAX)];
    return size.height;
}


- (CGFloat)totalHeightOfTableViewWithArray:(NSMutableArray *)arr {
    
    // 总高度
    CGFloat totalHeight = 0;
    
    for (int i = 0; i < arr.count; i++) {
        XJTieZiDetailChildCommentModel *model = arr[i];
        
        NSString *str = [self commentStrWithModel:model];
        
        CGFloat h = [self heightForStr:str];
        
        totalHeight += h;
    }

    return totalHeight;
}

#pragma mark - 七 偷懒:控制UIButton的显示状态:只显示图片 还是只显示 删除文字
/**取出本地的 customer_id*/
- (NSString *) rb_customId{
   return [[NSUserDefaults standardUserDefaults] objectForKey:@"customer_id"];
}

// 这个判断逻辑很复杂啊
/**
 如果没有登录: 去登陆 、显示 评论图标
 登录: 一样   显示 删除文字
      不一样  显示 评论图标
 */

@end
